


public class Josephus {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] nums = new Josephus().JosephusFunction(5,2);
		for(int i:nums)
			System.out.print(i+"   ");
	}

	public int[] JosephusFunction(int sum,int part){ 	
		if(sum > 0 && part > 0){
			int[] result = new int[sum];
			
			int[] data = new int[sum];
			for(int i = 0;i < data.length;i++){
				data[i] = i+1;
			}
			
			int step = 0;
			for(int count = 0;count < sum;count ++){
				for(int i = 0;i < part - 1;i++){
					if(data[(++step)%sum] > 0){
						;
					}else{
						i--;
					}
				}
				result[count] = data[step%sum];
				data[step%sum] = 0;
				while(data[(++step)%sum] < 1 && count < sum -1);
			}
			
			return result;
		}
		return new int[0];
	}
}
